/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBean;


import Merlion.CRM.entity.BillingAddressEntity;
import Merlion.CRM.entity.ClientEntity;
import Merlion.CRM.entity.LineEntryEntity;

//import CRM.ProductCRMEntity;
import Merlion.CRM.entity.PricebookEntity;
import Merlion.CRM.bean.SalesLeadModuleRemote;
import Merlion.CRM.bean.SalesOrderProcessingModuleRemote;
import Merlion.CRM.entity.ShippingAddressEntity;
import Merlion.SCM.entity.ProductEntity;
import Merlion.util.state.AddressState;
import Merlion.util.state.ClientState;
import Merlion.util.state.ContactState;
import Merlion.util.state.LineEntryState;
import Merlion.util.state.ProductState;
import Merlion.util.state.SalesDocumentState;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import org.primefaces.event.RowEditEvent;


@ManagedBean(name="QuotationManagedBean")
@ViewScoped
public class QuotationManagedBean implements Serializable {

    private LineEntryState lineEntry = new LineEntryState();   
    private List<LineEntryState> lineEntryList = new ArrayList<LineEntryState>();
    private String quotationId;
    private Long quotationID;
    private Long clientID;
    private Date dateIssued;
    private Date date1;
    private Date date2;
    private Date validUntil;
    private String description;
    private Long   inquiryInput;
    
    private SalesDocumentState newQuotation = new SalesDocumentState();
    private List<SalesDocumentState> quotationList;
    private SalesDocumentState selectedQuotation;
    private SalesDocumentState[] selectedQuotations;
    private ClientEntity client;

    private List<ProductState> productList;
    private SalesDocumentState selectedQuotation1;
    private SalesDocumentState selectedViewQuotation;
    private List<ClientState> clientList;
    private List<LineEntryState> selectedProductList;
    private Date minimumDate;
    private String dateString;
    private List<SalesDocumentState> convertedQuotations;
    private List<SalesDocumentState> inquiryList;
    
    private String billingStreet;
    private String billingState;
    private String billingCity;
    private String billingCode;
    private String billingCountry;
    private Long   billingId;
    
    private String shippingStreet;
    private String shippingState;
    private String shippingCity;
    private String shippingCode;
    private String shippingCountry;
    private Long   shippingId;
    
    private String docName;
    private Date dueDate;
    private String shippingMethod;
    private String shippingRule;
    private String invoiceRule;
    private String paymentMethod;
    private String freightCostRule;
    private String currency;
    private boolean editMode;

    private Long productId;
    private int quantity;
    private double listPrice;
    private String type;
    private PricebookEntity pricebook;
    private List<PricebookEntity> pricebookList;
    private double totalPrice;
    private String pricingType;
    private List<ContactState> contactList;
    private List<AddressState> billingAddress;
    private List<AddressState> shippingAddress;
    
    @EJB
    private SalesOrderProcessingModuleRemote sop;
    @EJB
    private SalesLeadModuleRemote smb;
    
    public String reinit(){

        lineEntry = new LineEntryState();
        
        return null;
    }

    public Long getInquiryInput() {
        return inquiryInput;
    }

    public void setInquiryInput(Long inquiryInput) {
        this.inquiryInput = inquiryInput;
    }

    public List<SalesDocumentState> getInquiryList() {
        if(inquiryList == null){
            inquiryList = sop.viewSalesInquiries();
        }
        return inquiryList;
    }

    public void setInquiryList(List<SalesDocumentState> inquiryList) {
        this.inquiryList = inquiryList;
    }

    
    
    public List<LineEntryState> getSelectedProductList() {
        return selectedProductList;
    }

    public String getDateString() {
        return dateString;
    }

    public void setDateString(String dateString) {
        this.dateString = dateString;
    }

    
    
    public void setSelectedProductList(List<LineEntryState> selectedProductList) {
        this.selectedProductList = selectedProductList;
    }

    public List<ProductState> getProductList() {
        if (productList == null) {
            productList = sop.viewProductList();
        }
        return productList;
    }

    public void setProductList(List<ProductState> productList) {
        this.productList = productList;
    }

    public Date getMinimumDate() {
        return minimumDate;
    }

    public void setMinimumDate(Date minimumDate) {
        this.minimumDate = minimumDate;
        SimpleDateFormat df  = new SimpleDateFormat("dd/MM/yyyy");
        this.dateString = df.format(minimumDate);
  
    }

    
     public List<ClientState> getClientList() {
        if (clientList == null) {
            clientList = smb.viewClients();
        }
        return clientList;
    }

    public void setClientList(List<ClientState> clientList) {
        this.clientList = clientList;
    }


    public SalesDocumentState getSelectedViewQuotation() {
        return selectedViewQuotation;
    }

    public void setSelectedViewQuotation(SalesDocumentState selectedViewQuotation) {
        this.selectedViewQuotation = selectedViewQuotation;
        if(selectedViewQuotation!=null){System.out.println("View Quotation NOT NULL");}
        this.selectedViewQuotation = selectedViewQuotation;
        System.out.println("ID : "+selectedViewQuotation.getId());
        selectedProductList=sop.viewProductListforSalesDocument(selectedViewQuotation.getId());
        this.setSelectedProductList(selectedProductList);
        handleOnClickPO();
    }

    public SalesDocumentState getNewQuotation() {
        return newQuotation;
    }

    public void setNewQuotation(SalesDocumentState newQuotation) {
        this.newQuotation = newQuotation;
    }
    
    public void convertedInitView(PhaseEvent event){
        FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        if(convertedQuotations == null)
        {
            convertedQuotations = sop.viewConvertedQuotations();
        }
    }

    public List<SalesDocumentState> getConvertedQuotations() {
        return convertedQuotations;
    }

    public void setConvertedQuotations(List<SalesDocumentState> convertedQuotations) {
        this.convertedQuotations = convertedQuotations;
    }
    
    

    public void initView2(PhaseEvent event){
        FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        if(quotationList == null)
        {
            quotationList = sop.viewQuotations();
        }
    }
    
    public void initView(PhaseEvent event){
        FacesContext.getCurrentInstance().getExternalContext().getSession(true);
    }

    public QuotationManagedBean() {

        lineEntry = new LineEntryState();

    }

    public SalesDocumentState getSelectedQuotation1() {
        return selectedQuotation1;
    }

    public void setSelectedQuotation1(SalesDocumentState selectedQuotation1) {
        this.selectedQuotation1 = selectedQuotation1;
    }

    
       
    public LineEntryState getLineEntry() {
        return lineEntry;
    }

    public void setLineEntry(LineEntryState lineEntry) {
        this.lineEntry = lineEntry;
    }

    public List<LineEntryState> getLineEntryList() {
        System.out.println("GET LINE ENTRY LIST");
        return lineEntryList;
    }

    public void setLineEntryList(List<LineEntryState> lineEntryList) {
        System.out.println("SET LINE ENTRY LIST");
        this.lineEntryList = lineEntryList;
    }
    
    
    public void handleDateChange(){
        System.out.println("HANDLE DATE CHANGe");
        SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        String dateString1 = df.format(getDate1());
        System.out.println(dateString1);
        this.setDateString(dateString1);
    }
    
    public void addQuotation(ActionEvent event){
  
        System.out.println("Customer ID is : "+clientID);
  
        Long contactId = newQuotation.getContactId();
        String docName = newQuotation.getDocumentName();
        Date dateIssued = newQuotation.getDateIssued();
        Date validUntil = newQuotation.getValidUntil();
        Long clientId = newQuotation.getClientId();
        Long quotationId = newQuotation.getId();
        String description = newQuotation.getDescription();
        String pType = newQuotation.getPricingType();
        if(!lineEntryList.isEmpty()){System.out.println("Products not null");
         System.out.println("ID is : "+lineEntryList.get(0).getId());
        }
        System.out.println(":::WEB::: Calling addQuotation2()");
        sop.addQuotation(dateIssued, clientId,contactId,  validUntil, lineEntryList, description, docName, pricingType);
        System.out.println(":::WEB::: Add Quotation 2 done");

        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("quotationSuccessfulRegistration.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
    }
    
    public void onEditRow (RowEditEvent event){
              
        System.out.println(":::WEB::: On Edit Row");
        SalesDocumentState eDocument = (SalesDocumentState)event.getObject();
        
        if(eDocument!=null){
 
            System.out.println(":::WEB::: Edited document not null");
 
            String eDocName = eDocument.getDocumentName();
            String eStatus =  eDocument.getStatus();
            Date eDateIssued = eDocument.getDateIssued();
            Date eValidUntil = eDocument.getValidUntil();
            Long clientId=eDocument.getClientId();
            Long eQuotationId=eDocument.getId();
            String eDescription = eDocument.getDescription();
            int indicativeLeadTime  = eDocument.getIndicativeLeadTime();
            System.out.println(":::WEB::: Calling Edit Quotation");
            sop.editQuotation(eQuotationId, eDateIssued, clientId, eValidUntil, indicativeLeadTime, eDescription
                    ,eDocName, eStatus);
        }
        else{
            System.out.println(":::WEB::: Edited quotation is null");
        }
        
    }
     
    
      public void onEditLineEntry (RowEditEvent event){
              
        System.out.println(":::WEB::: On Edit Line Entry");
        LineEntryState line = (LineEntryState)event.getObject();
        
        if(line!=null){
            System.out.println(":::WEB::: Edited line entry not null");
            int indicativeLeadTime = line.getIndicativeLeadTime();
            int amountShipped = line.getAmountShipped();
            int amountInvoiced= line.getAmountInvoiced();
            Long id = line.getId();
            System.out.println(":::WEB::: Calling Update Lead Time");
            sop.updateLineEntry(id, indicativeLeadTime, amountShipped, amountInvoiced);
        }
        else{
            System.out.println(":::WEB::: Edited line entry is null");
        }
        
    }
     
    
    public void deleteQuotations() {
        System.out.println(":::WEB::: Delete Quotations");

        if (selectedQuotations != null) {
            System.out.println(":::WEB::: If selected quotations not null");
            for (int i = 0; i < selectedQuotations.length; i++) {
                System.out.println("selectedQuotations i: " + i + " - " + selectedQuotations[i].getId());
                sop.deleteQuotation(selectedQuotations[i].getId());
            }
        }else{
            System.out.println(":::WEB::: Selected quotations are null");
        }
        
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("quotations.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
    }
    
    public void cancelDelete() {
        for (SalesDocumentState sdState : selectedQuotations) {
            sdState = null;
        }
        if(selectedQuotations==null){
            System.out.println(":::WEB::: Deletion of quotations cancelled");
 
       }else{
            System.out.println(":::WEB::: Deletion of quotations are not cancelled");
        }
           
    }

    public boolean isEditMode() {
        return editMode;
    }

    public void setEditMode(boolean editMode) {
        this.editMode = editMode;
    }
    
    
    
    public void delete(ActionEvent Event){
        
        System.out.println(":::WEB::: Delete Quotation"); 
        
        if(selectedQuotation!=null){
            System.out.println(":::WEB::: Selected quotation not null");
            Long quotationId=selectedQuotation.getId();      
            System.out.println(":::WEB::: Quotation ID is : "+quotationId);
            sop.deleteQuotation(quotationId);
            System.out.println(":::WEB::: Deletion done");
        }
        else{
            System.out.println(":::WEB::: Selected quotation is null");
        }
        
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("quotations.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
    }

    public ClientEntity getClient() {
        return client;
    }

    public void setClient(ClientEntity client) {
        this.client = client;
    }

    public Long getClientID() {
        return clientID;
    }

    public void setClientID(Long clientID) {
        this.clientID = clientID;
    }

    public Date getDate1() {
        return date1;
    }

    public void setDate1(Date date1) {
        System.out.println("SET DATE 1");
        this.date1 = date1;
    }

    public Date getDate2() {
        return date2;
    }

    public void setDate2(Date date2) {
        this.date2 = date2;
    }

    public Date getDateIssued() {
        return dateIssued;
    }

    public void setDateIssued(Date dateIssued) {
        System.out.println("SET DATE ISSUED");
        this.setMinimumDate(dateIssued);

        this.dateIssued = dateIssued;
    }

    public Long getQuotationID() {
        return quotationID;
    }

    public void setQuotationID(Long quotationID) {
        this.quotationID = quotationID;
    }

    public String getQuotationId() {
        return quotationId;
    }

    public void setQuotationId(String quotationId) {
        this.quotationId = quotationId;
    }

    public List<SalesDocumentState> getQuotationList() {
        return quotationList;
    }

    public void setQuotationList(List<SalesDocumentState> quotationList) {
        this.quotationList = quotationList;
    }

    public SalesDocumentState getSelectedQuotation() {
        return selectedQuotation;
    }

    public void setSelectedQuotation(SalesDocumentState selectedQuotation) {
        System.out.println(":::WEB::: SET SELECTED QUOTATION");
        this.selectedQuotation = selectedQuotation;
    }

    public SalesDocumentState[] getSelectedQuotations() {
        return selectedQuotations;
    }

    public void setSelectedQuotations(SalesDocumentState[] selectedQuotations) {
        System.out.println("::WEB::: SET SELECTED QUOTATIONS");
        this.selectedQuotations = selectedQuotations;
    }

    public Long getBillingId() {
        return billingId;
    }

    public void setBillingId(Long billingId) {
        this.billingId = billingId;
    }

    public Long getShippingId() {
        return shippingId;
    }

    public void setShippingId(Long shippingId) {
        this.shippingId = shippingId;
    }
    
    

    public Date getValidUntil() {
        return validUntil;
    }

    public void setValidUntil(Date validUntil) {
        this.validUntil = validUntil;
    }

    public String getDescription() {
        
        return description;
    }

    public void setDescription(String description) {
        if(description.equals("")){
            billingCode="No description found.";
        }
        this.description = description;
    }

    public String getBillingCity() {
        return billingCity;
    }

    public void setBillingCity(String billingCity) {
        if(billingCity.equals("")){
            billingCity=" ";
        }
        this.billingCity = billingCity;
    }

    public String getBillingCode() {
        return billingCode;
    }

    public void setBillingCode(String billingCode) {
        if(billingCode.equals("")){
            billingCode=" ";
        }
        this.billingCode = billingCode;
    }

    public String getBillingCountry() {
        return billingCountry;
    }

    public void setBillingCountry(String billingCountry) {
        if(billingCountry.equals("")){
            billingCountry=" ";
        }
        this.billingCountry = billingCountry;
    }

    public String getBillingState() {
        return billingState;
    }

    public void setBillingState(String billingState) {
        if(billingState.equals("")){
            billingState=" ";
        }
        this.billingState = billingState;
    }

    public String getBillingStreet() {
        return billingStreet;
    }

    public void setBillingStreet(String billingStreet) {
        if(billingStreet.equals("")){
            billingStreet=" ";
        }
        this.billingStreet = billingStreet;
    }

    public String getShippingCity() {
        return shippingCity;
    }

    public void setShippingCity(String shippingCity) {
        if(shippingCity.equals("")){
            shippingCity=" ";
        }
        this.shippingCity = shippingCity;
    }

    public String getShippingCode() {
        return shippingCode;
    }

    public void setShippingCode(String shippingCode) {
        if(shippingCode.equals("")){
            shippingCode=" ";
        }
        this.shippingCode = shippingCode;
    }

    public String getShippingCountry() {
        return shippingCountry;
    }

    public void setShippingCountry(String shippingCountry) {
        if(shippingCountry.equals("")){
            shippingCountry=" ";
        }
        this.shippingCountry = shippingCountry;
    }

    public String getShippingState() {
        return shippingState;
    }

    public void setShippingState(String shippingState) {
        if(shippingState.equals("")){
            shippingState=" ";
        }
        this.shippingState = shippingState;
    }

    public String getShippingStreet() {
        return shippingStreet;
    }

    public void setShippingStreet(String shippingStreet){ 
            if(shippingStreet.equals("")){
            shippingStreet=" ";
        }
        this.shippingStreet = shippingStreet;
    }

    
    public void convertQuotation(ActionEvent event) {

        if (selectedViewQuotation.getStatus().equals("Approved")) {
            Long cId = selectedViewQuotation.getId();
            System.out.println("Quote ID " + cId);

            Long billId = Long.parseLong("0");
            Long shipId = Long.parseLong("0");
            
            ClientState clState = smb.searchClient(selectedViewQuotation.getClientId());
            if (clState.getClientType().equals("CU")) {
                if (billingId != null) {
                    System.out.println("Billing ID not null!");
                    smb.editBillingAddress(billingId, billingStreet, billingCity, billingState, billingCode, billingCountry);
                    billId = billingId;
                }

                if (shippingId != null) {
                    System.out.println("Shipping ID not null!");
                    smb.editShippingAddress(shippingId, shippingStreet, shippingCity, shippingState, shippingCode, shippingCountry);
                    shipId = shippingId;
                }
            }else{
                BillingAddressEntity ba = smb.addBillingAddress(billingStreet, billingCity, billingState, billingCode, billingCountry);
                ShippingAddressEntity sa = smb.addShippingAddress(shippingStreet, shippingCity, shippingState, shippingCode, shippingCountry);
                billId = ba.getId();
                shipId = sa.getId();
            }
            sop.convertQuotationToPurchaseOrder(cId, billId, shipId, description,
                    dueDate, docName, shippingMethod, "Availability", "After Delivery", 
                    paymentMethod, "Freight Cost Included.",
                    "SGD");

            try {
                FacesContext.getCurrentInstance().getExternalContext().redirect("quotations.xhtml");
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        } else {
            System.out.println("Status not approved!");
            FacesContext context = FacesContext.getCurrentInstance();  
          
            context.addMessage(null, new FacesMessage("Quotation Conversion not successful!", "Status has not been approved."));  
       
        }
 
    }

    public String getCurrency() {
        return currency;
    }

    public void setCurrency(String currency) {
    if(currency.equals("")){
                currency="-";
            }
        this.currency = currency;
    }

    public String getDocName() {
        return docName;
    }

    public void setDocName(String docName) {
        if(docName.equals("")){
            docName="-";
        }
        this.docName = docName;
    }

    public Date getDueDate() {
        return dueDate;
    }

    public void setDueDate(Date dueDate) {
        this.dueDate = dueDate;
    }

    public String getFreightCostRule() {
  
        return freightCostRule;
    }

    public void setFreightCostRule(String freightCostRule) {
              if(freightCostRule.equals("")){
            freightCostRule="Freight Cost included.";
        }
        this.freightCostRule = freightCostRule;
    }

    public String getInvoiceRule() {
        return invoiceRule;
    }

    public void setInvoiceRule(String invoiceRule) {
        if(invoiceRule.equals("")){
            invoiceRule="After Delivery";
        }
        this.invoiceRule = invoiceRule;
    }

    public String getPaymentMethod() {
        return paymentMethod;
    }

    public void setPaymentMethod(String paymentMethod) {
        if(paymentMethod.equals("")){
            paymentMethod.equals("On Credit");
        }
        this.paymentMethod = paymentMethod;
    }

    public String getShippingMethod() {
        return shippingMethod;
    }

    public void setShippingMethod(String shippingMethod) {
        if(shippingMethod.equals("")){
            shippingMethod = "Land";
        }
        this.shippingMethod = shippingMethod;
    }

    public String getShippingRule() {
        return shippingRule;
    }

    public void setShippingRule(String shippingRule) {
        if(shippingRule.equals("")){
            shippingRule = "Availability";
        }
        this.shippingRule = shippingRule;
    }
    
     public PricebookEntity getPricebook() {
        
        return pricebook;
    }
    
    public void setPricebook(PricebookEntity pricebook) {
        System.out.println("SET PRICEBOOK");
        
        this.pricebook = pricebook;
        
    }
    
    public double getTotalPrice() {
        return totalPrice;
    }
    
    public void setTotalPrice(double totalPrice) {
        System.out.println("Set total price");
        this.totalPrice = totalPrice;
    }
    
    public String getPricingType() {
        return pricingType;
    }
    
    public void setPricingType(String pricingType) {
        this.pricingType = pricingType;
        lineEntry.setPricingType(pricingType);
        newQuotation.setPricingType(pricingType);
    }
    
    public List<PricebookEntity> getPricebookList() {
        System.out.println("GET PRICEBOOK LIST");
        
        if (pricebookList == null) {            
   
                System.out.println("GET from bean");
                pricebookList = sop.getPricebookList();

        }
        return pricebookList;
    }
    
    public void setPricebookList(List<PricebookEntity> pricebookList) {
        System.out.println("SET pricebook List");
        this.pricebookList = pricebookList;
    }
    
    public double getListPrice() {
        return listPrice;
    }
    
    public void setListPrice(double listPrice) {
        System.out.println("SET LIST PRICE");
        this.listPrice = listPrice;
    }
    
    public void handlePricebookChange() {
        System.out.println("HANDLE PRICEBOOK CHANGE");
        if (lineEntry.getPricebookId() != null) {
            System.out.println("Pricebook not null");
            System.out.println("Type " + type);
            System.out.println("quantity" + lineEntry.getQuantity());
            
            listPrice = sop.getListPrice(lineEntry.getQuantity(), type, lineEntry.getPricebookId());
            lineEntry.setPrice(listPrice);
            System.out.println(lineEntry.getPrice());
            double total = sop.getTotalPrice(lineEntry, pricingType);
            System.out.println("Total " + total);
            lineEntry.setTotalPrice(total);
        }
    }
    
    public void onRowEdit(RowEditEvent event) {
        
        System.out.println(":::WEB::: On Edit Row");
        LineEntryState eLine = (LineEntryState) event.getObject();
         int eQuantity = eLine.getQuantity();
         double newPrice = eLine.getPrice();
        eLine.setPrice(newPrice);
        eLine.setQuantity(eQuantity);
        
        double total = sop.getTotalPrice(eLine, pricingType);
        
        eLine.setTotalPrice(total);
        
    }
    
    public void handleProductChange() {
        System.out.println("HANDLE PRODUCT CHANGE");
        if (lineEntry.getItemId() != null) {
            List<PricebookEntity> list = sop.getPricebook(lineEntry.getItemId());
            System.out.println("Product is not empty");
            this.setPricebookList(list);
            ProductState product = sop.searchProduct(lineEntry.getItemId());
            lineEntry.setItemName(product.getItemName());
            lineEntry.setBasePrice(product.getPrice());            
        }
    }
    
    public void handleOnClickPO(){
        System.out.println("handle on click Po");
        if(selectedViewQuotation!=null){
            System.out.println("selected quotation not null");
            Long clientId = selectedViewQuotation.getClientId();
            ClientState clState = smb.searchClient(clientId);
            if (clState.getClientType().equals("CU")) {
                System.out.println("Set billing and shipping address");
                List<AddressState> addressList = clState.getAddressList();
             
                List<AddressState> billingAddressList = new ArrayList<AddressState>();
                List<AddressState> shippingAddressList = new ArrayList<AddressState>();
                List<ContactState> cList = smb.viewContactsForClients(clState.getId());
                System.out.println(cList.size());
                for (Object o : addressList) {
                    AddressState as = (AddressState) o;
                    if (as.getType().equals("B")) {
                        billingAddressList.add(as);
                    } else if (as.getType().equals("S")) {
                        shippingAddressList.add(as);
                    }
                }
  
            this.setBillingAddress(billingAddressList);
            this.setShippingAddress(shippingAddressList);
        }else{
                this.setBillingAddress(sop.getBillingAddressList());
                this.setShippingAddress(sop.getShippingAddressList());
            }}
    }
    
    public Long getProductId() {
        return productId;
    }
    
    public void setProductId(Long productId) {
        this.productId = productId;
    }
    
    public int getQuantity() {
        return quantity;
    }
    
    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }
    
    public String getType() {
        return type;
    }
    
    public void setType(String type) {
        this.type = type;
    }

    public void handleClientChange() {
        System.out.println("Handle client change");
        if (newQuotation.getClientId() !=null) {
            ClientState clState = smb.searchClient(newQuotation.getClientId());
            String bType = clState.getBusinessType();
            this.setType(bType);
            List<ContactState> cList =smb.viewContactsForClients(newQuotation.getClientId());
            this.setContactList(cList);
        }
    }

    public void handlePriceChange() {
        System.out.println("Handle price change");
        if (lineEntry.getPrice() != 0) {
            System.out.println("Price " + lineEntry.getPrice());
            double total = sop.getTotalPrice(lineEntry, pricingType);
            lineEntry.setTotalPrice(total);
        }
    }
    

    public List<ContactState> getContactList() {
        if(contactList==null){
            contactList = smb.viewContacts();
        }
        return contactList;
    }

    public void setContactList(List<ContactState> contactList) {
        this.contactList = contactList;
    }

    public List<AddressState> getBillingAddress() {
        if(billingAddress==null){
            billingAddress = sop.getBillingAddressList();
        }
        return billingAddress;
    }

    public void setBillingAddress(List<AddressState> billingAddress) {
        this.billingAddress = billingAddress;
    }

    public List<AddressState> getShippingAddress() {
        if(shippingAddress==null){
            shippingAddress = sop.getShippingAddressList();
        }
        return shippingAddress;
    }

    public void setShippingAddress(List<AddressState> shippingAddress) {
        this.shippingAddress = shippingAddress;
    }
    
    public void handleBillingAddressChange(){
        System.out.println("Handle billing Address Change");
            AddressState ba = sop.searchBillingAddress(billingId);
            this.setBillingStreet(ba.getStreet());
            this.setBillingCity(ba.getCity());
            this.setBillingState(ba.getState());
            this.setBillingCode(ba.getCode());
            this.setBillingCountry(ba.getCountry());
    
    }
    
     public void handleShippingAddressChange(){
        System.out.println("Handle Shipping Address Change");
            AddressState sa  = sop.searchShippingAddress(shippingId);
            this.setShippingStreet(sa.getStreet());
            this.setShippingCity(sa.getCity());
            this.setShippingState(sa.getState());
            this.setShippingCode(sa.getCode());
            this.setShippingCountry(sa.getCountry());
        }

    public void handleSalesDocumentChange() {
        System.out.println("HANDLE SALES DOCUMENT CHANGE");
        if (inquiryInput != null) {
            SalesDocumentState inquiryState = sop.searchInquiry(inquiryInput);
            Long salesDocClient = inquiryState.getClientId();
            Long contactId = inquiryState.getContactId();
            System.out.println("Sales doc client : " + salesDocClient);
            newQuotation.setClientId(salesDocClient);
          
            ClientState clState = smb.searchClient(newQuotation.getClientId());
            String bType = clState.getBusinessType();
            this.setType(bType);
            String pType = newQuotation.getPricingType();
            this.setPricingType(pType);
            newQuotation.setPricingType(pType);

            handleClientChange();

            System.out.println("Getting line entry list from sales inquiry");
            Collection<LineEntryEntity> collectionList = inquiryState.getLineEntryList();
            List<LineEntryEntity> list = new ArrayList<LineEntryEntity>(collectionList);
            List<LineEntryState> newList = new ArrayList<LineEntryState>();
            for (Object o : list) {
                LineEntryEntity line = (LineEntryEntity) o;
                LineEntryState a = new LineEntryState();
                a.setBasePrice(line.getBasePrice());
                a.setItemId(line.getItemId());
                a.setTotalPrice(line.getTotalLinePrice());
                a.setQuantity(line.getQuantity());
                a.setPrice(line.getPrice());
                a.setPricebookId(line.getPricebookId());
                a.setIndicativeLeadTime(line.getLeadTime());
                a.setItemName(line.getItemName());
                lineEntryList.add(a);
                newList.add(a);
            }
   
            handleProductChange();
            handlePricebookChange();
            handlePriceChange();
         

        }
    }
}
